import { getUserInfo, login, getPermission } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
export default {
  namespaced: true,
  state: {
    token: getToken(),
    user: {},
    userInfo: {},
    permission: {}
  },
  mutations: {
    set_token(state, token) {
      state.token = token
      setToken(token)
    },
    set_user(state, user) {
      state.user = user
    },
    set_userInfo(state, info) {
      state.userInfo = info
    },
    set_permission(state, permission) {
      state.permission = permission
    },
    remove_Token(state) {
      state.token = ''
      removeToken()
    },
    remove_userInfo(state) {
      state.userInfo = {}
    }
  },
  actions: {
    async login(context, data) {
      const res = await login(data)
      // console.log(res)
      context.commit('set_token', res.data.jwt)
      context.commit('set_user', res.data.user)
    },
    async getUserInfo(context) {
      const res = await getUserInfo()
      // console.log(res.data)
      context.commit('set_userInfo', res.data)
    },
    async getPermission(context) {
      const res = await getPermission()
      // console.log(res)
      context.commit('set_permission', res.data)
    },
    logout(context) {
      context.commit('remove_Token')
      context.commit('remove_userInfo')
    }
  }
}

